package sample.panel;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import sample.dao.JDO;
import sample.dao.LowLevelAPI;
import sample.dto.Child;
import sample.dto.Parent;
import sample.model.DatastoreFormModel;
import sample.page.ReadPage;

@SuppressWarnings("serial")
public class CreatePanel extends Panel{

	public CreatePanel(String WicketID,final PageParameters pram) {
		super(WicketID);
		
		final int UseDao = pram.getInt("Dao");
		
        DatastoreFormModel model= new DatastoreFormModel();
        Form<Void> DatastoreForm = new Form<Void>("DatastoreForm");
        final TextField<String> RootKeyText = new TextField<String>("Rootkey",new PropertyModel<String>(model,"Rootkey"));
        final TextField<String> RootValueText = new TextField<String>("RootValue",new PropertyModel<String>(model,"RootValue"));
        final TextField<String> EntityKeyText = new TextField<String>("EntityKey",new PropertyModel<String>(model,"EntityKey"));
        final TextField<String> EntityValueText1 = new TextField<String>("EntityValue1",new PropertyModel<String>(model,"EntityValue1"));
        final TextField<String> EntityValueText2 = new TextField<String>("EntityValue2",new PropertyModel<String>(model,"EntityValue2"));
        Button PreservationButton = new Button("Preservation"){
			@Override
			public void onSubmit() {
				super.onSubmit();
				
				if(RootKeyText.getInput().equals("")){
					System.out.println("ルートが空白");
				}else if(EntityKeyText.getInput().equals("")){
					
					Key RootKey = KeyFactory.createKey("Parent",RootKeyText.getInput());
					Parent ParentEntity = new Parent(RootKey,RootValueText.getInput());
					
					if(UseDao == 0){
						JDO dao = new JDO();
						long time=System.currentTimeMillis();
						dao.Create(ParentEntity,null);
						System.out.println("time:"+(System.currentTimeMillis()-time)+"miils");
					}else{
						LowLevelAPI LowLevelDao = new LowLevelAPI();
						long time=System.currentTimeMillis();
						LowLevelDao.SaveParent(ParentEntity);
						System.out.println("time:"+(System.currentTimeMillis()-time)+"miils");
					}
					
					System.out.println("Rootのみ保存");
					
					setResponsePage(ReadPage.class, pram);
				}else{
					
					Key RootKey = KeyFactory.createKey("Parent",RootKeyText.getInput());
					Parent ParentEntity = new Parent(RootKey,RootValueText.getInput()); 
					
					Key EntityKey = KeyFactory.createKey(RootKey,"Child",EntityKeyText.getInput());
					Child ChildEntity = new Child(EntityKey,EntityValueText1.getInput(),EntityValueText2.getInput());
					
					if(UseDao == 0){
						JDO dao = new JDO();
						dao.Create(ParentEntity, ChildEntity);
					}else{
						LowLevelAPI LowLevelDao = new LowLevelAPI();
						LowLevelDao.SaveParent(ParentEntity);
						LowLevelDao.SaveChild(ChildEntity);
					}

					System.out.println("エンティティグループの保存");
					
					setResponsePage(ReadPage.class, pram);
				}
			}
        };
        DatastoreForm.add(RootKeyText);
        DatastoreForm.add(RootValueText);
        DatastoreForm.add(EntityKeyText);
        DatastoreForm.add(EntityValueText1);
        DatastoreForm.add(EntityValueText2);
        DatastoreForm.add(PreservationButton);
        add(DatastoreForm);
	}
}


